import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;


public class ImgTools 
{
	public static BufferedImage getBytes2BufferedImage(byte[] image, String format) throws IOException
	{
		if(image == null){return null;}
		BufferedImage data = null;
		
		ByteArrayInputStream baos = new ByteArrayInputStream(image);
		BufferedInputStream bos = new BufferedInputStream(baos);
		
		data = ImageIO.read(bos);
		
		
		bos.close();
		baos.close();
		
		return data;
	}
	
	public static byte[] getBufferedImage2Bytes(BufferedImage image, String format) throws IOException
	{

		if(image == null){return null;}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BufferedOutputStream bos = new BufferedOutputStream(baos);



		ImageIO.write(image, format, bos);
		bos.flush();
		bos.close();

		byte [] data = baos.toByteArray();
		baos.flush();
		baos.close();

		return data;
	}
	
	
	
}
